﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Apworks.Bus;
using Apworks.Config;
using Apworks.Application;
using TinyForum.Domain.Events;
using Apworks;
using Apworks.Repositories;
using TinyForum.Infrastructure;
using System.Linq.Expressions;
using Apworks.Specifications;

namespace TinyForum.Application
{
    /// <summary>
    /// Represents the base class for the application services.
    /// </summary>
    public abstract class ApplicationServiceBase
    {
        #region Private Fields
        private readonly IMessageDispatcher messageDispatcher;
        private readonly IConfigSource configSource = AppRuntime.Instance.CurrentApplication.ConfigSource;
        #endregion

        #region Ctor
        /// <summary>
        /// Initializes a new instance of <c>ApplicationServiceBase</c> class.
        /// </summary>
        public ApplicationServiceBase()
        {
            messageDispatcher = MessageDispatcher.CreateAndRegister(configSource, typeof(MessageDispatcher));
            messageDispatcher.DispatchFailed += this.OnDispatchEventFailed;
            messageDispatcher.Dispatched += this.OnDispatchEventSuccess;
        }
        #endregion

        #region Protected Methods
        /// <summary>
        /// Executes when the message was failed to be dispatched.
        /// </summary>
        /// <param name="sender">The instance of the message dispatcher.</param>
        /// <param name="e">The event data.</param>
        protected virtual void OnDispatchEventFailed(object sender, MessageDispatchEventArgs e) { }
        /// <summary>
        /// Executes when the message was dispatched successfully.
        /// </summary>
        /// <param name="sender">The instance of the message dispatcher.</param>
        /// <param name="e">The event data.</param>
        protected virtual void OnDispatchEventSuccess(object sender, MessageDispatchEventArgs e)
        {

        }
        /// <summary>
        /// Dispatches the event.
        /// </summary>
        /// <typeparam name="TEvent">The type of the event to be dispatched.</typeparam>
        /// <param name="event">The instance of the event to be dispatched.</param>
        protected virtual void DispatchEvent<TEvent>(TEvent @event)
            where TEvent : TinyForumDomainEvent
        {
            messageDispatcher.DispatchMessage(@event);
        }

        protected virtual TSingle GetSingle<TSingle>(Expression<Func<TSingle, bool>> spec)
            where TSingle : AggregateRoot
        {
            using (RepositoryContextManager mgr = new RepositoryContextManager())
            {
                IRepository<TSingle> repository = mgr.GetRepository<TSingle>();
                TSingle single = repository.Get(Specification<TSingle>.Eval(spec));
                return single;
            }
        }

        protected virtual TSingle GetSingle<TSingle>(Expression<Func<TSingle, bool>> spec, Action<TSingle> action)
            where TSingle : AggregateRoot
        {
            using (RepositoryContextManager mgr = new RepositoryContextManager())
            {
                IRepository<TSingle> repository = mgr.GetRepository<TSingle>();
                TSingle single = repository.Get(Specification<TSingle>.Eval(spec));
                action(single);
                return single;
            }
        }

        protected virtual TSingle GetSingle<TSingle>(Expression<Func<TSingle, bool>> spec, Action<TSingle, IRepositoryContext, IRepository<TSingle>> action)
            where TSingle : AggregateRoot
        {
            using (RepositoryContextManager mgr = new RepositoryContextManager())
            {
                IRepository<TSingle> repository = mgr.GetRepository<TSingle>();
                TSingle single = repository.Get(Specification<TSingle>.Eval(spec));
                action(single, mgr.Context, repository);
                return single;
            }
        }

        protected virtual TResult GetSingle<TSingle, TResult>(Expression<Func<TSingle, bool>> spec, Func<TSingle, TResult> func)
            where TSingle : AggregateRoot
        {
            using (RepositoryContextManager mgr = new RepositoryContextManager())
            {
                IRepository<TSingle> repository = mgr.GetRepository<TSingle>();
                TSingle single = repository.Get(Specification<TSingle>.Eval(spec));
                return func(single);
            }
        }

        protected virtual TResult GetSingle<TSingle, TResult>(Expression<Func<TSingle, bool>> spec, Func<TSingle, IRepositoryContext, IRepository<TSingle>, TResult> func)
            where TSingle : AggregateRoot
        {
            using (RepositoryContextManager mgr = new RepositoryContextManager())
            {
                IRepository<TSingle> repository = mgr.GetRepository<TSingle>();
                TSingle single = repository.Get(Specification<TSingle>.Eval(spec));
                return func(single, mgr.Context, repository);
            }
        }
        #endregion
    }
}
